<?php

namespace app\common\service\driver;

use app\common\constant\driving\DriverMoneyLogConstant;
use app\common\model\driving\DriverMoneyLog;
use think\Db;
use think\Exception;

/**
 * 司机余额变动服务类
 **/
class DriverMoneyLogService
{

    /**
     * 列表
     *
     * @param integer $page 页数
     * @param integer $page_size 每页数量
     * @param integer $type 类型
     * @param string $date 日期
     * @param mixed $user 用户信息
     * @return array
     */
    public static function list($page, $page_size, $type, $date, $user)
    {
        $page_size = $page_size > 0 ? $page_size : config('paginate.list_rows');
        if (!($page >= 1)) $page = 1;
        $offset = ($page - 1) * $page_size;

        $where = [];
        $where['driver_id'] = $user['id'];
        $type_list = DriverMoneyLogConstant::get_type_list();
        if (array_key_exists($type, $type_list)) {
            $where['type'] = $type;
        }
        if ($date) {
            $mk_time = mk_time(6, explode('-', $date));
            $where['createtime'] = ['BETWEEN', [date('Y-m-d H:i:s', $mk_time['start']), date('Y-m-d H:i:s', $mk_time['end'])]];
        }
        $total = DriverMoneyLog::where($where)->count('id');
        $data = [];
        if (!$total) {
            return compact("total", "data");
        }
        $list = DriverMoneyLog::where($where)
            ->order('id desc')
            ->page($page, $page_size)
            ->select();
        $data = $list;
        return compact("total", "data");
    }

}
